<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use Mail;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests\AdminloginRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class HomeLoginController extends Controller
{
    public function getLogin()
    {
        return view('home.login.login');
    }
    
    public function postLogingo(Request $request)
    {

        //判断用户名是否存在
        $name = DB::table('user')->where('username',$request->input('username'))->first();
        if($name){
        	//判断状态
        	if($name->status != 2){
        		return back()->with('error','用户未激活,请去邮箱激活');
        	}
            //判断密码是否正确
            if(Hash::check($request->input('password'),$name->password)){
               session(['name'=>$name->username]);
               session(['login'=>'欢迎您,'.$name->username]);
               session(['id'=>$name->id]);
               session(['qx'=>$name->qx]);
                return redirect('/');
            }else{
                return back()->with('error','用户名或密码不正确');
            }
        }else{
            return back()->with('error','用户名或密码不正确');
        }    
    }

    //用户退出
    public function getLoginout()
    {
        session()->forget('name');
        session()->forget('id');
        session()->forget('qx');
        //跳转至首页 kj
        return redirect('/');
    }
}
